package com.java46.struts2.Food.service;


import com.java46.struts2.Food.dao.FoodDao;
import com.java46.struts2.FoodType.dao.FoodTypeDao;
import com.java46.struts2.base.entity.PageEntity;
import com.java46.struts2.base.entity.TDishesEntity;
import com.java46.struts2.base.exception.BusinessException;

import java.math.BigDecimal;
import java.util.List;
/**
 * @ClassName FoodService
 * @Description 菜品的service
 * @Author Tigger Wu
 * @Date 2018/7/16 19:33
 * @Version 1.0
 **/
public class FoodService {
    private FoodDao foodDao = new FoodDao();

    /*
     *  查询全部菜品
     *
     * @author Tigger Wu
     * @date 2018/7/17 14:43
     * @param
     * @return
     */

    public List<TDishesEntity> queryAll(){
        List<TDishesEntity> slist = foodDao.queryAll();
        if(slist==null){
            return  null;
        }
        return slist;
    }
    /*
     *  添加菜品
     *
     * @author Tigger Wu
     * @date 2018/7/17 14:43
     * @param
     * @return
     */

    public  void addFood(TDishesEntity foods){
    if(foods==null){
        throw new BusinessException("添加失败！");
    }
        foodDao.addFood(foods);

    }

    /*
     *  删除菜品
     *
     * @author Tigger Wu
     * @date 2018/7/17 14:44
     * @param
     * @return
     */
//    public  void removeFoods(int id){
//        if("".equals(id)){
//            throw new BusinessException("考虑下");
//        }
//        foodDao.removeFoods(id);
//    }
/*
 *  修改菜品
 *
 * @author Tigger Wu
 * @date 2018/7/17 14:50
 * @param
 * @return
 */
    public void updateFoods(TDishesEntity foods){
        if(foods==null){
            throw new BusinessException("空的");
        }
        foodDao.updateFoods(foods);
    }
    /**
     *  查询类型下全部菜品
     *
     * @author TiggerWu
     * @date 2018/7/18  11:03
     * @param
     * @return
     */
    public Integer[] queryFoodsByTypeId(int id){
        if("".equals(id)){
            throw  new BusinessException("cc");
        }
        Integer[] arr = foodDao.queryFoodsId(id);
        return arr;
    }
    /**
     *  根据id查询菜品
     *
     * @author TiggerWu
     * @date 2018/7/18  17 :21
     * @param
     * @return
     */
    public List<TDishesEntity> querybyId(int id){
        if("".equals(id)){
            throw new BusinessException("g");
        }
        List<TDishesEntity> foods =  foodDao.queryById(id);
        return foods;
    }

    /**
     *@Author xiao
     *@Description //分页查询菜品类型可加关键字
     *@Date 9:53 2018/7/18
     *@Param [Pageno,Pagesize,Keyword]
     *@return PageEntity
     */
    public static PageEntity queryFoods(String Pageno, String Pagesize, String Keyword){
        int pagesize=0;
        int pageno=0;
        //如果为空
        if (Pagesize==null||Pagesize.trim().equals("")){
            pagesize=8;
        }else {
            try {
                //转换出错
                pagesize=Integer.parseInt(Pagesize);
            }catch (Exception e){
                pagesize=8;
            }
        }
        try{
            //转换出错
            pageno=Integer.parseInt(Pageno);
        }catch (Exception e){
            pageno=1;
        }

        List list = FoodDao.queryFood(Keyword,pageno,pagesize);
        if(list.isEmpty()){
            throw new BusinessException("没有查询到任何菜品类型数据");
        }
        PageEntity page=new PageEntity();
        //数据集合
        page.setList(list);
        int count=FoodDao.queryFoodsCount(Keyword);
        //总条数
        page.setTotalNumber(count);
        //页码
        page.setPageNo(pageno);
        //每页条数
        page.setPageSize(pagesize);
        //总页数
        page.setPageCount((page.getTotalNumber()%page.getPageSize()==0)?page.getTotalNumber()/page.getPageSize():page.getTotalNumber()/page.getPageSize()+1);
        return page;
    }

    /**
     *@Author xiao
     *@Description //根据菜品删除订单
     *@Date 11:57 2018/7/18
     *@Param [Typeid]//菜品编号
     *@return void
     */
    public static void deleteFood(String foodid){
        int foodId;
        if(foodid==null||foodid.trim().equals("")){
            throw new BusinessException("菜品编号为空");
        }
        try {
            foodId=Integer.parseInt(foodid);
        }catch (Exception e){
            throw  new BusinessException("菜品编号格式不正确");
        }
        //删除菜品表里的数据
        FoodDao.deleteFood(foodId);

    }

    /**
     * @Author xiaobo
     * @Description //添加菜品类型
     * @Date 16:58 2018/7/22 0022
     * @Param [Typename]
     * @return void
     */
//    public static void addFood(String foodname,String price,String remark,String file){
//        if(Typename==null||Typename.trim().equals("")){
//            throw new BusinessException("菜品类型名称为空");
//        }
//        //删除菜品类型单表里的数据
//        FoodTypeDao.addType(Typename);
//    }


}
